package com.atguigu.tingshu.account.receiver;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * projectName: com.atguigu.tingshu.account.receiver
 *
 * @author: 杨天伦
 * description:
 * time: 2025/6/27 1:55
 */
@Component
@Slf4j
public class AccountReceiver {

    @Autowired
    UserAccountService userAccountService;
    /**
     * 用户注册成功
     * @param userId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_USER_REGISTER,
            durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_USER,
            durable = "true", autoDelete = "false"),key = MqConst.ROUTING_USER_REGISTER
    ))
    public void register(Long userId, Message message, Channel channel) {
        System.out.println("用户注册成功：" + userId);
        //判断消息是否存在
        try {
            if (null !=userId){
                userAccountService.initAccount(userId);
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        //手动签收
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

}
